<template>
    <div>

        <el-form ref="search" :model="search" label-width="80px">
            <el-col :span="8">
                &nbsp;
            </el-col>
            <el-form-item label="患者姓名">
                <el-col :span="5">
                    <el-input v-model="search.name" placeholder="患者姓名"></el-input>
                </el-col>
                <el-col :span="1">
                    <el-button @click="getSearch()"><i class="el-icon-search"></i></el-button>
                </el-col>
            </el-form-item>
        </el-form>
        <el-table :data="seeadocterList" style="width: 100%" border stripe>
            <el-table-column prop="sid" label="编号" width="180"> </el-table-column>
            <el-table-column prop="pname" label="姓名" width="180"> </el-table-column>
            <el-table-column prop="psex" label="性别"> </el-table-column>
            <el-table-column prop="patbir" label="出生日期"> </el-table-column>
            <el-table-column prop="phone" label="联系方式"> </el-table-column>
            <el-table-column prop="date" label="诊断时间"> </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="assignRoleShow(scope.row)">办理住院</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页功能 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="pageInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
        </el-pagination>

        <el-dialog title="住院办理" :visible.sync="dialogVisible" width="40%" :before-close="handleClose"
            style="text-align: left">
            <el-form ref="inpatient" :model="inpatient" label-width="80px" :rules="rules">
                <el-form-item label="患者名称">
                    <el-input v-model="inpatient.pname" disabled></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-input v-model="inpatient.psex" disabled></el-input>
                </el-form-item>
                <el-form-item label="出生日期">
                    <el-input v-model="inpatient.patbir" disabled></el-input>
                </el-form-item>
                <el-form-item label="住院科室" prop="kid">
                    <el-select v-model="inpatient.kid" placeholder="请选择科室" @change="keshichange()">
                        <el-option v-for="keshi in keshiList" :key="keshi.kid" :label="keshi.kname" :value="keshi.kid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="医生" prop="did">
                    <el-select clearable v-model="inpatient.did" placeholder="请选择医生" id="docterSel">
                        <el-option v-for="doctor in doctorList" :key="doctor.did" :label="doctor.dname"
                            :value="doctor.did">
                        </el-option>

                    </el-select>
                </el-form-item>
                <el-form-item label="住院病房" prop="srid">
                    <el-select v-model="inpatient.srid" placeholder="请选择病房" @change="roomchange(inpatient.srid)">
                        <el-option v-for="sickroom in sickroomList " :key="sickroom.srid" :label="sickroom.srid"
                            :value="sickroom.srid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="病房费用">
                    <el-input v-model="inpatient.srprice" disabled></el-input>
                </el-form-item>
                <el-form-item label="住院病床" prop="bid">
                    <el-select v-model="inpatient.bid" placeholder="请选择病床" id="sickbedSel">
                        <el-option v-for="sickbed in sickbedList " :key="sickbed.bid" :label="sickbed.bname"
                            :value="sickbed.bid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="住院时间" prop="inpdate">
                    <el-date-picker type="date" placeholder="选择日期" v-model="inpatient.inpdate"
                        style="width: 100%;"></el-date-picker>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm">取 消</el-button>
                <el-button type="primary" @click="assignRole">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
//导出
export default {
    data() {
        return {
            //搜索栏
            search: {},
            //住院信息
            inpatient: {},
            //模态框关闭
            dialogVisible: false,
            //获得所有的诊断记录信息
            seeadocterList: [],
            //科室信息
            keshiList: [],
            //所有医生信息
            doctorList: [],
            //选中的医生信息
            doctorSelectKey: 0,
            //全部病房信息
            sickroomList: [],
            //病床信息
            sickbedList: [],
            //病床费用
            sickroomcost: "",
            //校验
            rules: {
                kid: [
                    { required: true, message: '请选择科室', trigger: 'change' }
                ],
                did: [
                    { required: true, message: '请选择医生', trigger: 'change' }
                ],
                srid: [
                    { required: true, message: '请选择病房', trigger: 'change' }
                ],
                bid: [
                    { required: true, message: '请选择病床', trigger: 'change' }
                ],
                inpdate: [
                    { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                ],
            },
            //分页
            pageInfo: {
                pageNum: 1,
                pageSize: 5
            },
        };
    },
    methods: {
        getSearch() {
            if (this.search.name == "") {
                this.list();
            } else {
                this.$axios.get("seeadoctor/getSearch?pageNum=" + this.pageInfo.pageNum + "&pageSize=" + this.pageInfo.pageSize + "&search=" + this.search.name)
                    .then((result) => {
                        let vo = result.data;
                        if (vo.code == 200) {
                            this.seeadocterList = vo.data.list;
                        }
                    }).catch((err) => {
                        console.log(err);
                    });
            }
        },
        handleSizeChange(val) {
            this.pageInfo.pageSize = val;
            this.list();
        },
        handleCurrentChange(val) {
            this.pageInfo.pageNum = val;
            this.list();
        },
        resetForm() {
            this.doctorList = [];
            this.sickbedList = [];
            this.$refs['inpatient'].resetFields();
            this.dialogVisible = false;
        },
        assignRole() {
            this.$refs['inpatient'].validate((valid) => {
                if (valid) {
                    this.$axios.post("inpatient/getPidUpdate", this.inpatient)
                        .then((result) => {
                            let vo = result.data;
                            this.$message(vo.message);
                            if (vo.code == 200) {
                                this.$axios.get("asset/updateIn?pid=" + this.inpatient.pid + "&cost=" + this.inpatient.srprice)
                                    .then((result) => {
                                        let vo = result.data;
                                        this.$message(vo.message);
                                        if (vo.code == 200) {
                                        }
                                    }).catch((err) => {
                                        console.log(err);
                                    });
                            }
                            this.doctorList = [];
                            this.sickbedList = [];
                            this.$refs['inpatient'].resetFields();
                            this.dialogVisible = false;
                            this.list();
                        }).catch((err) => {
                            console.log(err);
                        });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        assignRoleShow(row) {
            this.inpatient.sid = row.sid;
            this.inpatient.pid = row.pid;
            this.inpatient.pname = row.pname;
            this.inpatient.psex = row.psex;
            this.inpatient.patbir = row.patbir;
            this.dialogVisible = true;
        },
        onSubmit() {
            console.log('submit!');
        },
        handleClose() {
            this.doctorList = [];
            this.sickbedList = [];
            this.$refs['inpatient'].resetFields();
            this.dialogVisible = false;
        },
        keshichange() {
            //根据kid查医生
            this.$axios.get("docter/getKid?kid=" + this.inpatient.kid)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.doctorList = vo.data;
                        document.getElementById("docterSel").value = "";
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        roomchange(srid) {
            document.getElementById("sickbedSel").value = "";
            //查询病房信息
            this.$axios.get("sickroom/getKey?srid=" + srid)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.inpatient.srprice = vo.data.srprice;
                    }
                }).catch((err) => {
                    console.log(err);
                });
            //查询病房的所有病床信息
            this.$axios.get("sickbedroom/getSickbed?srid=" + srid)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.sickbedList = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },
        list() {
            //查所有待住院病人
            this.$axios.get("seeadoctor/getInhosp?pageNum=" + this.pageInfo.pageNum + "&pageSize=" + this.pageInfo.pageSize)
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.seeadocterList = vo.data.list;
                        this.pageInfo = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
            //查所有科室
            this.$axios.get("keshi/getAll")
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.keshiList = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
            //查所有病房
            this.$axios.get("sickroom/getAll")
                .then((result) => {
                    let vo = result.data;
                    if (vo.code == 200) {
                        this.sickroomList = vo.data;
                    }
                }).catch((err) => {
                    console.log(err);
                });
        },

    },
    mounted() {
        this.list();
    },
};
</script>
<style></style>